Plotting chord-diagram with R

Michael Nostalgie

2018-11-09

Chord diagram

  • A graphical method of displaying the inter-relationships between data in a matrix.
  • The data are arranged radially around a circle with the relationships between the data points typically drawn as arcs connecting the data.

circlize - a R package for circular visualization

Intro

Coordinate transformation

  • Data coordinate systems
  • Polar coordinate systems
  • Canvas coordinate systems

Layout

Rules of using circlize to make circular plot

How to use circlize?

circos.initialize(factors, xlim)

circos.track(factors, ylim)

for(sector.index in all.sector.index) {
    circos.points(x1, y1, sector.index)
    circos.lines(x2, y2, sector.index)
}

...

An example

Step 1 - Generate random data

set.seed(999)
n = 1000
df = data.frame(factors = sample(letters[1:8], n, replace = TRUE),
    x = rnorm(n), y = runif(n))
dplyr::tbl_df(df)
## # A tibble: 1,000 x 3
##    factors        x      y
##    <fct>      <dbl>  <dbl>
##  1 d       -0.338   0.228 
##  2 e        0.201   0.835 
##  3 a        0.603   0.651 
##  4 g       -0.225   0.861 
##  5 g       -0.0101  0.362 
##  6 a       -0.194   0.525 
##  7 e        0.00919 0.780 
##  8 a        1.44    0.674 
##  9 d        0.175   0.0761
## 10 e        0.472   0.969 
## # ... with 990 more rows

Step 2 - Initialize a circular layout

suppressMessages(library(circlize))
circos.par("track.height" = 0.1)
circos.initialize(factors = df$factors, x = df$x)

Step 3 - Add a track and drawing plot

circos.track(factors = df$factors, y = df$y,
    panel.fun = function(x, y) {
        circos.text(CELL_META$xcenter, CELL_META$cell.ylim[2] + uy(5, "mm"), CELL_META$sector.index)
        circos.axis(labels.cex = 0.6)
})
col = rep(c("#FF0000", "#00FF00"), 4)
circos.trackPoints(df$factors, df$x, df$y, col = col, pch = 16, cex = 0.5)
circos.text(-1, 0.5, "text", sector.index = "a", track.index = 1)

Step 4 - Add a track and drawing plot

bgcol = rep(c("#EFEFEF", "#CCCCCC"), 4)
circos.trackHist(df$factors, df$x, bin.size = 0.2, bg.col = bgcol, col = NA)

Repeat Step 3 / Step 4

More about using circlize to visualize your data

Linking genomic regions

Representation of transcript isoforms

Nested zooming

Resources

  1. Circlize document
  2. R graph gallery

Thank you!